package com.hddz.comlib.packet;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.util.Log;

import com.hddz.comlib.R;
import com.hddz.comlib.model.Sensor;
import com.hddz.comlib.util.ConvertUtility;

public class WeekUpPacket extends Packet implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected static final ThreadLocal<SimpleDateFormat> dateformat = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyyMMddHHmmss");
		};
	};
	private static final String TAG = "WeekUpPacket";
	/**
	 * 站号
	 */
	protected int stationId;
	/**
	 * 发送时间
	 */
	protected Date sendTime;
	/**
	 * 传感器数据
	 */
	protected Sensor sensor;

	public int getStationId() {
		return stationId;
	}

	public Date getSendTime() {
		return sendTime;
	}

	public Sensor getSensor() {
		return sensor;
	}

	@Override
	public String createViewText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("站号:" + this.stationId);
		sb.append(" 时间:" + dateformat.get().format(this.sendTime));
		sb.append(" 电池电压:" + this.sensor.getValue() + this.sensor.getChannel());
		sb.append("\r\n");
		return sb.toString();
	}

	@Override
	public Boolean parse(List<Byte> data, int start, int len) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Boolean parse(ByteBuffer data, int len) {
		// TODO Auto-generated method stub
		byte[] b = new byte[len];
		int perPosition = data.position();
		try {
			data.get(b, 0, len);
			this.sourceText = ConvertUtility.bystesToStr(b, 0, len);
			Log.d("WeekUpPacket_收到=>>", this.sourceText);
			data.position(perPosition);
			stationId = ((data.get() & 0xff) * 256 + (data.get() & 0xff)) * 256 + (data.get() & 0xff);
			Date date = new Date();
			sendTime = dateformat.get().parse(
					String.format("%04d", date.getYear() + 1900) + String.format("%02d", date.getMonth() + 1)
							+ String.format("%02d", ConvertUtility.BCDToInt(data.get() & 0xff)) + ConvertUtility.BCDToInt(data.get() & 0xff)
							+ String.format("%02d", date.getMinutes()) + String.format("%02d", date.getSeconds()));
			sensor = new Sensor();
			sensor.setStationId(stationId);
			sensor.setChannel("V");
			int v = data.get() & 0xff;
			if (v > 100) {
				sensor.setValue(v * 0.02);// 电池电压解数据时，先乘以2，再除以100
			} else {
				sensor.setValue(v / 10.0);// 数值 <= 100，解析方法：数值÷10；
			}
			this.viewText = this.createViewText();
			Log.d(TAG, this.viewText);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public String getDataAdd(int resid) {
		String data = "";
		if (resid == R.id.rb_station_item_stationno) {
			data = String.format("%08d", getStationId());
		} else if (resid == R.id.rb_station_item_voltage) {
			data = String.format("%.2f", sensor.getValue());
		} else if (resid == R.id.rb_station_item_time) {
			data = dateformat.get().format(sendTime);
		} else {
			data = "-";
		}
		return data;
	}
}
